# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.pipelines.images import ImagesPipeline
# from xiangmu.proxyhelper import ProxyHelper
from xiangmu.mysqlhelper import MysqlHelper
from scrapy.http import Request
from scrapy.utils.python import to_bytes
import hashlib
class XiangmuPipeline(object):
    def process_item(self, item, spider):
        return item

class doubanImagesPipline(ImagesPipeline):
    def get_media_requests(self, item, info):
        request_list = []
        for x in item.get(self.images_urls_field, []):
            req = Request(x)
            req.meta['move_name'] = item['title']
            request_list.append(req)
        return request_list

        # return [Request(x) for x in item.get(self.images_urls_field, [])]

    def file_path(self, request, response=None, info=None):
        ## start of deprecation warning block (can be removed in the future)
        def _warn():
            from scrapy.exceptions import ScrapyDeprecationWarning
            import warnings
            warnings.warn('ImagesPipeline.image_key(url) and file_key(url) methods are deprecated, '
                          'please use file_path(request, response=None, info=None) instead',
                          category=ScrapyDeprecationWarning, stacklevel=1)

        # check if called from image_key or file_key with url as first argument
        if not isinstance(request, Request):
            _warn()
            url = request
        else:
            url = request.url

        # detect if file_key() or image_key() methods have been overridden
        if not hasattr(self.file_key, '_base'):
            _warn()
            return self.file_key(url)
        elif not hasattr(self.image_key, '_base'):
            _warn()
            return self.image_key(url)
        ## end of deprecation warning block

        image_guid = hashlib.sha1(to_bytes(url)).hexdigest()  # change to request.url after deprecation
        return 'full/%s.jpg' % (request.meta['move_name'])


class MysqlPipeline(object):
    def __init__(self):
        self.helper = MysqlHelper()

    def process_item(self, item, spider):
        if 'get_inset_sql_and_data' in dir(item):

            (insert_sql, data) = item.get_inset_sql_and_data()
            self.helper.execute_modify_sql(insert_sql,data)

        return item